
const http = require('http')
const querystring = require('querystring')
const url = require('url')


const server = http.createServer((req, res) => {
  const { method } = req;
  let path = '', get = {}, post = {};
  console.log(method);
  
  if(method === 'GET'){
    const {pathName,query} = url.parse(req.url,true);
    path = pathName;
    get = query;   
    complete()
  }else if(method === 'POST') {
    path = req.url;
    let bufferArr = [];
    req.on('data',(buffer) => {
      bufferArr.push(buffer)
    })
    req.on('end',()=>{
      const buffer = Buffer.concat(bufferArr)
      post = querystring.parse(buffer.toString())
      complete()
    })
  }
  function complete() {
    console.log(method, path, get, post)
  }
})
server.listen(8666)